package x86;

import hex.Instruction;
import hex.Operand;
import hex.Operator;

/**
 * CALL
 * @author qj
 *
 */
public class CALL extends Detector {
	public final static CALL INSTANCE = new CALL();

	public boolean detect() {
		int b = input.getByte();
		int b2 = input.prefetchByte();
		init(b, b2);

		// 1110 1000 : full displacement
		if (b == 0xE8) {
			new Instruction(Operator.CALL.INSTANCE, input.getLEDWORD(),5)
					.Add();
			return MATCHED;
		}
		// 1111 1111 : 11010reg
		if (b == 0xFF && (b2 >> 3 == 0x1A)) {
			new Instruction(Operator.CALL.INSTANCE,
					vm.GlobalRegister[modrm_rm],2).Add();
			return MATCHED;
		}

		// 1111 1111 : mod 010 r/m
		if (b == 0xFF && modrm_reg == 2) {
			Operand t = decode_modrm();
			new Instruction(Operator.LDM.INSTANCE, t,
					new Operand.NullOperand(), vm.TempRegister[1],0).Add();

			new Instruction(Operator.CALL.INSTANCE, vm.TempRegister[1],2)
					.Add();
			return MATCHED;
		}

		return NOT_MATCHED;
	}
}
